<?php
/**
 * @file /Db/Adapter/mysql.php
 *
 * mysql-Adapter to database.
 *
 * Class that represents a connection to a MySQL-Database and uses the common
 * functions defined by Db_Interface.
 *
 * New BSD License
 * \n
 * Copyright (c) 2010 Rpd-Team
 *
 * @namespace  rpd
 * @since      0.0.1
 * @version    0.0.1
 */

/**
 *
 */

class Db_Adapter_mysql extends Db_Adapter implements Db_Adapter_Interface {

	function encoding($charset)
	{
		mysql_query("set character set " . $charset);
	}

	function database($name) {
		return mysql_select_db($name, $this->_link);
	}

	function query($statement, $values = NULL)
	{
		if ($values !== NULL)
		{
			$values = array_map("mysql_real_escape_string", $values);
			$statement = vsprintf($statement, $values);
		}

		$result = mysql_query($statement, $this->_link);

		if ($result === true)
		{
			return true;
		}

		return new Db_Statement_mysql($result, $statement);
	}

	function insert_id()
	{
		return mysql_insert_id($this->_link);
	}

	function __construct($settings) {
		if ($settings["persistent"])
		{
			$this->_link = mysql_pconnect($settings["server"],
			                              $settings["user"],
			                              $settings["password"]);
		}
		else
		{
			$this->_link = mysql_connect($settings["server"],
			                             $settings["user"],
			                             $settings["password"]);
		}

		$this->database($settings["database"]);
	}

}

?>